package com.example.mymvvmdemo.models.article_detail;


import android.app.Activity;
import android.content.Intent;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.base.BaseLoadActivity;
import com.example.mymvvmdemo.constants.UrlConstants;
import com.example.mymvvmdemo.constants.UrlTypeConstants;
import com.example.mymvvmdemo.databinding.ActivityArticleDetailBinding;
import com.example.mymvvmdemo.dialog.LoadingAnimDialog;
import com.example.mymvvmdemo.models.main.fragment.home.entity.ArticlesDetailEntity;
import com.example.mymvvmdemo.net.presenter.GetPresenter;
import com.example.mymvvmdemo.net.presenter.PostBodyPresenter;
import com.example.mymvvmdemo.net.presenter.PostPresenter;
import com.example.mymvvmdemo.net.view.IView;
import com.example.mymvvmdemo.utils.LogUtils;
import com.example.mymvvmdemo.utils.StrUtils;
import com.example.mymvvmdemo.utils.ToastUtils;

import java.util.HashMap;

public class ArticleDetailActivity extends BaseLoadActivity<ActivityArticleDetailBinding> implements View.OnClickListener, IView.GetView, IView.PostBodyView, IView.PostView {

    public static void startArticleDetailActivity(Activity activity, int id) {
        Intent intent = new Intent(activity, ArticleDetailActivity.class);
        intent.putExtra("id", id);
        activity.startActivity(intent);
    }

    private ArticleDetailViewModel viewModel;
    private GetPresenter getPresenter;
    private PostPresenter postPresenter;
    private PostBodyPresenter postBodyPresenter;
    /*** 加载时候的Dialog */
    private LoadingAnimDialog loadingDialog;

    @Override
    protected void showDialog() {
        if (loadingDialog == null)
            loadingDialog = new LoadingAnimDialog(this, getResources().getString(R.string.load));
        if (!loadingDialog.isShowing()) loadingDialog.show(this);
    }

    @Override
    protected void dismissDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.close(this);
        }
    }

    @Override
    protected void initData() {

        viewModel = new ViewModelProvider(this).get(ArticleDetailViewModel.class);
        getPresenter = new GetPresenter(this);
        postPresenter = new PostPresenter(this);
        postBodyPresenter = new PostBodyPresenter(this);
        viewModel.id = getIntent().getIntExtra("id", 0);
        initTitle(getString(R.string.add_article));
        startLoadArticleDetail();

    }

    @Override
    protected void initListener() {
        binding.include.ivBack.setOnClickListener(this);
        binding.likeView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.like_view:
                changeLikeState();
                break;
            default:
                break;
        }
    }

    /***********************************自定义方法**************************************/
    public void initTitle(String title) {
        binding.include.tvTitle.setVisibility(View.VISIBLE);
        binding.include.tvTitle.setText(title);
    }

    private void startLoadArticleLike() {
        showDialog();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("id", "" + viewModel.id);
        postPresenter.startLoadPost(UrlConstants.articlesLike, hashMap, UrlTypeConstants.articlesLike);
    }

    private void startLoadArticleDetail() {
        showDialog();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("id", "" + viewModel.id);
        getPresenter.startLoadGet(UrlConstants.articlesDetail, hashMap, UrlTypeConstants.articlesDetail);
    }

    private void changeLikeState() {
        boolean isLiked = !binding.likeView.isLiked;
        binding.likeView.setLiked(isLiked);
        if (isLiked) {
            binding.likeView.setLikeCount(viewModel.likes + 1);
            startLoadArticleLike();
            binding.likeView.setEnabled(false);
        } else {
//            binding.likeView.setLikeCount(viewModel.likes);
        }
    }

    /***********************************数据请求**************************************/
    @Override
    public void loadGetSuccess(String result, String type, int code) {
        if (type.equals(UrlTypeConstants.articlesDetail)) {
            loadArticlesDetailSuccess(result, code);
        }
    }

    @Override
    public void loadGetFail(String message, String type) {
        if (type.equals(UrlTypeConstants.articlesDetail)) {
            loadArticlesDetailFail();
        }
    }

    @Override
    public void loadGetAccountError(int code) {

    }

    @Override
    public void loadGetNetWorkError() {
        dismissDialog();
        toast(R.string.network_error, ToastUtils.TOA_W);
    }

    @Override
    public void loadPostBodySuccess(String result, String type, int code) {
        if (type.equals(UrlTypeConstants.articlesLike)) {
            loadArticlesLikeSuccess(result, code);
        }
    }

    @Override
    public void loadPostBodyFail(String message, String type) {
        if (type.equals(UrlTypeConstants.articlesLike)) {
            loadArticlesLikeFail();
        }
    }

    @Override
    public void loadPostBodyAccountError(int code) {

    }

    @Override
    public void loadPostBodyNetWorkError(String type) {
        dismissDialog();
        toast(R.string.network_error, ToastUtils.TOA_W);
    }

    @Override
    public void loadPostSuccess(String result, String type, int code) {
        if (type.equals(UrlTypeConstants.articlesLike)) {
            loadArticlesLikeSuccess(result, code);
        }
    }

    @Override
    public void loadPostFail(String message, String type) {
        if (type.equals(UrlTypeConstants.articlesLike)) {
            loadArticlesLikeFail();
        }
    }

    @Override
    public void loadPostAccountError(int code) {

    }

    @Override
    public void loadPostNetWorkError() {
        dismissDialog();
        toast(R.string.network_error, ToastUtils.TOA_W);
    }

    /**************************************处理请求后数据***********************************************/
    private void loadArticlesLikeSuccess(String result, int code) {
        if (code != 200) {
            loadArticlesLikeFail();
            return;
        }
        dismissDialog();
    }

    private void loadArticlesLikeFail() {
        dismissDialog();
    }

    private void loadArticlesDetailSuccess(String result, int code) {
        dismissDialog();
        try {
            ArticlesDetailEntity articlesDetailEntity = gson.fromJson(result, ArticlesDetailEntity.class);
            if (code != 200) {
                loadArticlesDetailFail();
                return;
            }
            ArticlesDetailEntity.DataDTO data = articlesDetailEntity.getData();
            String title = data.getTitle();
            if (!StrUtils.isEmpty(title)) {
                binding.include.tvTitle.setText(title);
            }

            String desc = data.getContent();
            if (!StrUtils.isEmpty(desc)) {
                binding.tvContent.setText(desc);
            }

            String time = data.getCreated_at();
            if (!StrUtils.isEmpty(time)) {
                binding.tvTime.setText(time);
            }

            int likes = data.getLikes();
            viewModel.likes = likes;
            binding.likeView.setLikeCount(likes);

        } catch (Exception e) {
            LogUtils.e("loadArticlesDetailSuccess error:" + e.getMessage());
        }
    }

    private void loadArticlesDetailFail() {
        dismissDialog();
    }

}